package com.zd.kids.ui.activity;

import android.content.res.Configuration;
import android.view.OrientationEventListener;
import android.widget.LinearLayout;

import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.hyy.libray.presenter.IBasePresenter;
import com.hyy.libray.ui.activity.BaseActivity;
import com.zd.kids.DataBean.BookSeriesListDataBean;
import com.zd.kids.DataBean.BookUnitDataBean;
import com.zd.kids.R;
import com.zd.kids.net.CustomObserver;
import com.zd.kids.net.HttpMethod;
import com.zd.kids.ui.adapter.BookUnitListAdapter;
import com.zd.kids.ui.view.CommmonBottomHelper;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class BookUnitActivity extends BaseActivity {

    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.ll_rootview)
    LinearLayout llRootview;


    private BookUnitListAdapter bookUnitListAdapter;

    private String mGroupid;

    private String mCategoryid;

    /**
     * 获取布局文件地址
     */
    @Override
    public int getLayoutResId() {
        return R.layout.activity_book_unit;
    }


    /**
     * 初始化布局
     */
    @Override
    public void initView() {

        initTitle("", R.color.cartoon_bookunit_color);

        mGroupid = getIntent().getStringExtra(BookSeriesListActivity.GROUP_ID_KEY);
        mCategoryid = getIntent().getStringExtra(BookSeriesListActivity.CATEGORY_ID_KEY);

        CommmonBottomHelper commmonBottomHelper = new CommmonBottomHelper(this, llRootview);

        recycleView.setLayoutManager(new LinearLayoutManager(this));

        bookUnitListAdapter = new BookUnitListAdapter(new ArrayList<>());
        bookUnitListAdapter.setmGroupid(mGroupid);
        bookUnitListAdapter.setmCategoryid(mCategoryid);

        recycleView.setAdapter(bookUnitListAdapter);
    }


    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        ImmersionBar.with(this)
                .fullScreen(true)
                .hideBar(BarHide.FLAG_HIDE_BAR)
                .init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBookUnitData();
    }

    /**
     * 获取Presenter，不用MVP，可以为空
     *
     * @return
     */
    @Override
    protected IBasePresenter onBasePresenter() {
        return null;
    }

    private void getBookUnitData() {
        showLoading();
        HttpMethod.getIntence().getBookUnits(mGroupid, mCategoryid, new CustomObserver<BookUnitDataBean>(this) {
            @Override
            public void onSuccess(BookUnitDataBean bookUnitDataBean) {
                disLoading();
                BookUnitDataBean.ResultBean resultBean = new BookUnitDataBean.ResultBean();
                resultBean.setAiType(1);
                resultBean.setAiLock(!bookUnitDataBean.isAssess_open());
                resultBean.setAiComplete(bookUnitDataBean.isAssess_status());
                resultBean.setAssessState(bookUnitDataBean.getAssess_state());


                bookUnitDataBean.getResult().add(resultBean);

                bookUnitListAdapter.setNewData(bookUnitDataBean.getResult());

                tvTitle.setText(bookUnitDataBean.getCategory_name());
            }

            @Override
            public void onError(String errorCode) {
                disLoading();
            }
        });
    }
}
